package com.xiaozheng.dao.impl;

import com.xiaozheng.dao.AccountDao;
import com.xiaozheng.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * @Package: com.xiaozheng.dao.impl
 * @ClassName: AccountDaoImpl
 * @Author: 小政同学    QQ:xiaozheng666888@qq.com
 * @CreateTime: 2020/9/25 12:46
 * @What_is_this_file_for: 账户持久层实现类
 * @Description: 描述
 */
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {


    @Autowired
    private QueryRunner runner;


    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Account> findAllAcount() {
        try {
            return runner.query("select * from account", new BeanListHandler<Account>(Account.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /**
     * 根据id查询一个
     *
     * @return
     */
    @Override
    public Account findAccountById(Integer AccountId) {
        Account account = null;
        try {
            account = runner.query("select * from account where id = ?", new BeanHandler<Account>(Account.class), AccountId);
            return account;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return account;
    }

    /**
     * 保存
     *
     * @param account
     */
    @Override
    public void saveAccount(Account account) {
        try {
            runner.update("insert into account(uid,money)values(?,?)", account.getUid(), account.getMoney());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 更新
     *
     * @param account
     */
    @Override
    public void updateAccount(Account account) {
        try {
            runner.update("update account set uid = ?,money = ? where id = ?", account.getUid(), account.getMoney(),
                    account.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 根据id删除
     *
     * @param AccountId
     */
    @Override
    public void deleteAccount(Integer AccountId) {
        try {
            runner.update("delete from account where id = ?", AccountId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
